var answerCurrentIndex;
$(document).ready(function(){
	initQuestion();
});
//初始化问题
function initQuestion(){
	var id = window.location.href.split("?")[1];
	var pram = "";
	var url = "/HyperIT/question/getOneQuestion";
	if(getCookie("user")==null){
		parm = "question_id="+id;
	}else{
		parm = "question_id="+id+"&userdetail_id="+thisUser.id;
	}
	$.get(url,parm,function(data){
		if (data.serviceResult) {
			var queser = data.resultParm.user;
			queserId = queser.id;
			var question = data.resultParm.question;
			questionId = question.id;
			var tag = data.resultParm.tag;
			$("#queser").attr("href","../blog/theblog.html?"+queser.id);
			$("#queser img").attr("src","../.."+queser.head);
			$("#title").text(html_decode(question.title));
			for(var i = 0; i<tag.length; i++){
				$("#tags").append('<li><a href="../tag/thetag.html?'+tag[i].id+'">'+tag[i].name+'</a></li>');
			}
			isAccept = "";
			if (question.accept) {
				isAccept = "已采纳";
			} else{
				isAccept = "未采纳";
			}
			$("#answer").text(question.answer);
			$("#accept").text(isAccept);
			$("#reading").text(question.reading);
			$("#createtime").text(question.createtime.substr(0,16));
			$("#questionContent").append(html_decode(question.content));
			editormd.markdownToHTML("questionContent");
			initAnswer();
			initTheBestAnswer();
		} else{
			messageTip(data.resultInfo);
		}
	});
}
//去回答，显示mask
function toAnswer(){
	if(getCookie("user")==null){
		messageTip("哎呀，还没登录呢！");
		return;
	}
	$("#mask").show();
}
//取消回答或者回答成功关闭遮罩
function cancelAnswer(){
	$("#mask").hide();
}
//回答ajax
function answer(){
	if(getCookie("user")==null){
		messageTip("哎呀，还没登录呢！");
		return;
	}
	var url = "/HyperIT/question/saveAnswer";
	var parm = "content="+html_encode($("#editorhtml").val())+"&userDetail_id="+thisUser.id+"&question_id="+questionId;
	$.post(url,parm,function(data){
		if (data.serviceResult) {
			messageTip(data.resultInfo);
			cancelAnswer();
			$(".answers ul").prepend('<li id="answer_'+data.resultParm.answer_user.answer_id+'">'+
										'<a href="../blog/theblog.html?'+data.resultParm.answer_user.userdetail_id+'">'+
										'<img src="../..'+data.resultParm.answer_user.path+'"/></a>'+
										'<span>'+data.resultParm.answer_user.createtime.substr(0,16)+'</span>'+
										'<div id="answerContent_'+data.resultParm.answer_user.answer_id+'">'+
											html_decode(data.resultParm.answer_user.content)+
										'</div>'+
									'</li>');
			editormd.markdownToHTML("answerContent_"+data.resultParm.answer_user.answer_id);
		} else{
			messageTip(data.resultInfo)
		}
	});
}
function initAnswer(){
	$(".answers ul").children("li").remove();
	answerCurrentIndex=1;
	getAnswer(answerCurrentIndex);
}
function getAnswer(index){
	var url = "/HyperIT/question/getAnswer";
	var parm = "size=5&indexPageNum="+index+"&question_id="+questionId;
	$.get(url,parm,function(data){
		if (data.serviceResult) {
			var answerList = data.resultParm.answer_user_list;
			showAnswer(answerList);
			answerCurrentIndex++;
		} else{
			if (answerCurrentIndex==1) {
				return;
			} else{
				messageTip(data.resultInfo);
			}
		}
	});
}
//绘制
function showAnswer(list){
	for(var i = 0; i<list.length; i++){
		if(thisUser.id == queserId&&thisUser.id != list[i].userdetail_id&&isAccept=="未采纳"){
			$(".answers ul").append('<li id="answer_'+list[i].answer_id+'">'+
										'<a href="../blog/theblog.html?'+list[i].userdetail_id+'">'+
										'<img src="../..'+list[i].path+'"/></a>'+
										'<span>'+list[i].createtime.substr(0,16)+'</span>'+
										'<span onclick="toAccept(this)" class="acceptbtn">采纳</span>'+
										'<div id="answerContent_'+list[i].answer_id+'">'+
											html_decode(list[i].content)+
										'</div>'+
									'</li>');
		}else{
			$(".answers ul").append('<li id="answer_'+list[i].answer_id+'">'+
										'<a href="../blog/theblog.html?'+list[i].userdetail_id+'">'+
										'<img src="../..'+list[i].path+'"/></a>'+
										'<span>'+list[i].createtime.substr(0,16)+'</span>'+
										'<div id="answerContent_'+list[i].answer_id+'">'+
											html_decode(list[i].content)+
										'</div>'+
									'</li>');
		}
		editormd.markdownToHTML("answerContent_"+list[i].answer_id);
	}
}
function getMoreAnswer(){
	getAnswer(answerCurrentIndex);
}
//采纳答案
function toAccept(which){
	if(getCookie("user")==null){
		messageTip("哎呀，还没登录呢！");
		return;
	}
	var url = "/HyperIT/question/saveBestAnswer";
	var parm = "question_id="+questionId+"&answer_id="+$(which).parent().attr("id").split("_")[1];
	$.post(url,parm,function(data){
		if (data.serviceResult) {
			showTheBestAnswer(data.resultParm.answer_user);
			messageTip(data.resultInfo);
		} else{
			messageTip(data.resultInfo);
		}
	});
}
function showTheBestAnswer(au){
	$(".applyed").children().remove();
	$(".applyed").append('<a href="../blog/theblog.html?'+au.userdetail_id+'" id="theBestAA">'+
							  '<img src="../..'+au.path+'"/>'+
						 '</a>'+
						 '<span id="antime">'+au.createtime.substr(0,16)+'</span>'+
						 '<div id="applyedContent">'+
							  html_decode(au.content)+
						 ' </div>');
	editormd.markdownToHTML("applyedContent");
 }
function initTheBestAnswer(){
	var url = "/HyperIT/question/getBestAnswer";
	var parm = "question_id="+questionId;
	$.post(url,parm,function(data){
		if (data.serviceResult) {
			showTheBestAnswer(data.resultParm.answer_user);
		} else{
			return;
		}
	});
}
